package cn.virens.common.project.base.utils;

public class NumberUtils {

    public static Long sum(Number... nums) {
        Long answer = 0L;

        for (Number number : nums) {
            answer += l(number);
        }

        return answer;
    }

    public static Long sub(Number a, Number... b) {
        if (a == null) {
            return 0L;
        }

        Long answer = l(a);

        for (Number number : b) {
            answer -= l(number);
        }

        return answer;
    }

    public static Long mul(Number a, Number b) {
        if (a == null) {
            return 0L;
        }
        if (b == null) {
            return 0L;
        }

        return l(a) * l(b);
    }

    public static Long div(Number a, Number b) {
        if (a == null) {
            return 0L;
        }
        if (b == null) {
            return 0L;
        }

        return l(a) / l(b);
    }

    public static Long average(Long a, Long b) {
        if (a == null) {
            return l(b);
        }
        if (b == null) {
            return l(a);
        }

        return (l(a) + l(b)) / 2;
    }

    public static <T extends Number> boolean lessThen(T a, T b) {
        if (a == null) {
            return false;
        }
        if (b == null) {
            return true;
        }

        return f(a) < f(b);
    }

    public static <T extends Number> boolean greaterThen(T a, T b) {
        if (a == null) {
            return false;
        }
        if (b == null) {
            return true;
        }

        return f(a) > f(b);
    }

    private static long l(Number num) {
        return num != null ? num.longValue() : 0L;
    }

    private static float f(Number num) {
        return num != null ? num.floatValue() : 0L;
    }
}
